<template>
  <el-dialog
    v-model="visible"
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
  >
    <el-form
      ref="dataFormRef"
      :model="dataForm"
      :rules="dataRule"
      label-width="80px"
      @keyup.enter="onSubmit()"
    >
      <el-form-item label="账单用户类型" prop="userType">
        <el-input
          v-model="dataForm.userType"
          :disabled="true"
          placeholder="账单用户类型"
        />
      </el-form-item>

      <el-form-item label="账单用户ID" prop="uid">
        <el-input v-model="dataForm.uid" placeholder="账单用户ID" />
      </el-form-item>

      <el-form-item label="账单分类" prop="category">
        <el-input v-model="dataForm.category" :disabled="true" placeholder="账单分类" />
      </el-form-item>

      <el-form-item label="账单类型" prop="type">
        <el-radio-group v-model="dataForm.type">
          <el-radio :label="'level1'"> 一级奖金 </el-radio>
          <el-radio :label="'level2'"> 二级奖金 </el-radio>
          <el-radio :label="'level3'"> 三级奖金 </el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="增加/减少" prop="pm">
        <el-radio-group v-model="dataForm.pm">
          <el-radio :label="0"> 减 </el-radio>
          <el-radio :label="1"> 加 </el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="本次操作金额" prop="money">
        <el-input v-model="dataForm.money" placeholder="本次操作金额" />
      </el-form-item>

      <el-form-item label="剩余金额" prop="residueMoney">
        <el-input v-model="dataForm.residueMoney" placeholder="剩余金额" />
      </el-form-item>

      <el-form-item label="标题" prop="title">
        <el-input v-model="dataForm.title" placeholder="标题" />
      </el-form-item>

      <el-form-item label="详情" prop="details">
        <el-input v-model="dataForm.details" placeholder="详情" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="onSubmit()">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ElMessage } from "element-plus";
import { Debounce } from "@/utils/debounce";
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataForm = ref({
  id: "",
  userType: "tz_user",
  uid: "",
  category: "distribution_bonus",
  type: "",
  pm: 0, //0:减 1:加
  money: "",
  residueMoney: "",
  title: "",
  details: "",
});
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10, // 每页显示多少条
});
const dataRule = {
  nickName: [{ required: true, message: "用户名不能为空", trigger: "blur" }],
};

const dataFormRef = ref(null);
const init = (id) => {
  dataForm.value.id = id || 0;
  //dataForm.value = { userId: id } || {};
  visible.value = true;
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });
  if (dataForm.value.id) {
    http({
      url: http.adornUrl(`/user/fundDetail/info/${dataForm.value.id}`),
      method: "get",
      params: http.adornParams(),
    }).then(({ data }) => {
      dataForm.value = data;
    });
  }
};
defineExpose({ init });

/**
 * 表单提交
 */
const onSubmit = Debounce(() => {
  dataFormRef.value?.validate((valid) => {
    if (valid) {
      http({
        url: http.adornUrl("/user/fundDetail"),
        method: dataForm.value.id ? "put" : "post",
        data: http.adornData({
          ...dataForm.value,
        }),
      }).then(() => {
        ElMessage({
          message: "操作成功",
          type: "success",
          duration: 1500,
          onClose: () => {
            visible.value = false;
            emit("refreshDataList", page);
          },
        });
      });
    }
  });
});
</script>
